package dryven.sapi.jetty;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;

import dryven.config.ApplicationConfiguration;
import dryven.config.xml.XMLConfigFileParser;
import dryven.init.FrontControllerFactory;
import dryven.request.FrontController;
import dryven.sapi.servlet_v25.ServletApplicationConfiguration;
import dryven.sapi.servlet_v25.ServletRequest;
import dryven.sapi.servlet_v25.ServletResponse;

public class JettyHandler implements Handler {

	private FrontController _frontController;
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Server getServer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void handle(String target, Request baseRequest, HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		baseRequest.setHandled(true);
		
		if(_frontController==null) {
			_frontController = createFrontController();
		}
		
		dryven.request.http.Request dryvenRequest = new ServletRequest(request);
		dryven.request.http.Response dryvenResponse = new ServletResponse(response);
		_frontController.handleRequest(dryvenRequest, dryvenResponse);
	}

	@Override
	public void setServer(Server arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addLifeCycleListener(Listener arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isFailed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isRunning() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isStarted() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isStarting() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isStopped() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isStopping() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void removeLifeCycleListener(Listener arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void start() throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void stop() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	public FrontController createFrontController() {
		ApplicationConfiguration cfg = createConfiguration();
		FrontControllerFactory factory = null;
		try {
			factory = cfg.getFrontControllerFactoryClass().newInstance();
		} catch (Exception e) {
			throw new RuntimeException(String.format("Could not instanciate application delegate %s",cfg.getFrontControllerFactoryClass().getName(), e));
		}
		FrontController frontCtrl = factory.createFrontController(cfg);
		return frontCtrl;
	}
	
	protected ApplicationConfiguration createConfiguration() {
		InputStream doc = null;
		XMLConfigFileParser parser = new XMLConfigFileParser(doc);
		return new JettyBaseConfiguration(parser.parse());
	}

}
